package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import beans.B_Objetivo;
import conexion.Conexion;
import java.sql.Types;

/**
 *
 * @author JoseHernan
 */
public class DB_Objetivo {
    
    public static ArrayList<B_Objetivo> getObjetivo(int id) {
        ArrayList<B_Objetivo> list = new ArrayList<B_Objetivo>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;

            ps = con.prepareStatement(" select id, descripcion from tbl_objetivo where id = " + id);

            rs = ps.executeQuery();
            B_Objetivo obj;
            while (rs.next()) {
                obj = new B_Objetivo();
                //Asign values
                obj.setId(rs.getInt("id"));
                obj.setDescripcion(rs.getString("descripcion"));
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    public static ArrayList<B_Objetivo> getListByIdPlanAuditoria() {
        ArrayList<B_Objetivo> list = new ArrayList<B_Objetivo>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;

            ps = con.prepareStatement(" select o.id, o.descripcion from tbl_objetivo o inner join tbl_plan_auditoria pa on pa.id = o.id_plan_auditoria where pa.removed = false and pa.is_active = true and o.removed = false ");

            rs = ps.executeQuery();
            B_Objetivo obj;
            while (rs.next()) {
                obj = new B_Objetivo();
                //Asign values
                obj.setId(rs.getInt("id"));
                obj.setDescripcion(rs.getString("descripcion"));
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    //SP CRUD
    public static String crud(B_Objetivo obj, String action) {
        String result = "";
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_crud_objetivo(?,?,?,?,?,?)}");

            cs.registerOutParameter("p_message", Types.VARCHAR);
            cs.setString("p_action", action);
            cs.setInt("p_id", obj.getId());
            cs.setString("p_descripcion", obj.getDescripcion());
            cs.setInt("p_id_plan_auditoria", obj.getObjPlanAuditoria().getId());
            cs.setString("p_user", obj.getUser());
            cs.executeUpdate();
            result = cs.getString(1);
        } catch (Exception ex) {
            result = ex.getMessage();
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }
}
